﻿using System;
using System.Windows.Forms;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;
using DevExpress.XtraTab;
using ECP.Client.Utilities;
using ECP.Utilities;
using ECP.Utilities.Controls;

namespace ECP.Client
{
    public partial class MainRibbon : RibbonForm, IBaseMainForm
    {
        private SkinGalleryHelper skinGalleryHelper;

        public MainRibbon()
        {
            InitializeComponent();

            skinGalleryHelper = new SkinGalleryHelper(ribbonGallerySkins);
        }

        private void barSkin_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetSkin(e.Item.Caption);
        }

        private void SetSkin(string skinName)
        {
            DevExpress.LookAndFeel.UserLookAndFeel.Default.SetSkinStyle(skinName);
        }

        private void MainRibbon_Load(object sender, EventArgs e)
        {
            SetSkin("Blue");
            this.Text = BaseSystemInfo.SoftFullName;
            foreach (DevExpress.XtraBars.BarItem item in ribbonControl.Items)
            {
                item.ItemClick += new DevExpress.XtraBars.ItemClickEventHandler(item_ItemClick);
            }
        }

        private void ShowPageInfo(Object obj, string pageName)
        {
            var isCtl = obj as Control;
            var isform = obj as Form;

            XtraTabPage page = new XtraTabPage();
            foreach (XtraTabPage item in childTabPage.TabPages)
            {
                if (item.Name == pageName)
                {
                    childTabPage.SelectedTabPage = item;
                    return;
                }
            }
            isCtl.Dock = DockStyle.Fill;
            page.Name = pageName;
            page.Text = isCtl.Text;
            childTabPage.TabPages.Add(page);
            childTabPage.SelectedTabPage = page;
            if (isform is Form)
            {
                isform.Dock = DockStyle.Fill;
                isform.TopLevel = false;
                isform.FormBorderStyle = FormBorderStyle.None;
                isform.FormClosing += new FormClosingEventHandler(isform_FormClosing);
            }
            isCtl.Show();
            page.Controls.Add(isCtl);
        }

        private void isform_FormClosing(object sender, FormClosingEventArgs e)
        {
            Form frm = sender as Form;
            var page = frm.Parent as XtraTabPage;
            childTabPage.TabPages.Remove(page);
        }

        private void item_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var item = e.Item as IModelMenuCtl;

            if (string.IsNullOrEmpty(item.ModeleClass))
            {
                return;
            }
            var moudel = CacheManager.Instance.Getobject(item.ModelAssmely, item.ModeleClass) as IModelInfo;

            switch (moudel.DisplayType)
            {
                case DisplayTypes.TabPage:
                    ShowPageInfo(moudel, item.ModeleClass);
                    break;

                case DisplayTypes.Dialog:
                    var form = moudel as Form;
                    if (form != null)
                    {
                        form.ShowDialog();
                    }
                    break;

                case DisplayTypes.MDIChild:
                    var formChild = moudel as Form;
                    if (this.IsMdiContainer && formChild != null)
                    {
                        this.IsMdiContainer = true;
                        formChild.MdiParent = this;
                        formChild.Show();
                    }

                    break;
                case DisplayTypes.CallMethod:
                    moudel.StartModel();
                    break;
                case DisplayTypes.CommonShow:
                default:
                    var formcommon = moudel as Form;
                    if (formcommon != null)
                    {
                        formcommon.Show();
                    }

                    break;
            }
        }

        public void CheckMenu()
        {
            // throw new NotImplementedException();
        }

        public void InitForm()
        {
            //  throw new NotImplementedException();
        }

        public void InitService()
        {
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 退出系统
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainRibbon_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dlg = XtraMessageBox.Show(AppMessage.MSG0052, AppMessage.MSG0000, MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk);
            if (dlg != DialogResult.OK)
            {
                e.Cancel = true;
            }
        }

        private void childTabPage_CloseButtonClick(object sender, EventArgs e)
        {
            childTabPage.TabPages.Remove(childTabPage.SelectedTabPage);
        }
    }
}